import Bezier from 'bezier';

var GameData = require('../module/gameData')();

var movedata = [
    [
        [
            {
                "x": 133,
                "y": -420
            },
            {
                "x": 53,
                "y": -238
            },
            {
                "x": 777.8251599147122,
                "y": -2.0469083155650196
            },
            {
                "x": -192.40938166311298,
                "y": 52.196162046908285
            },
            {
                "x": -137.1428571428571,
                "y": 126.90831556503201
            },
            {
                "x": 329.5522388059703,
                "y": 338.76332622601285
            }
        ]
    ],
    [
        [
            {
                "x": 156.58848614072508,
                "y": -393.00639658848615
            },
            {
                "x": -82.89978678038369,
                "y": -242.55863539445627
            },
            {
                "x": -134.07249466950952,
                "y": -76.75906183368869
            },
            {
                "x": -63.45415778251595,
                "y": 128.95522388059703
            },
            {
                "x": 372.53731343283584,
                "y": -19.4456289978678
            },
            {
                "x": 219.01918976545858,
                "y": 197.5266524520256
            },
            {
                "x": -106.43923240938159,
                "y": 311.1300639658849
            },
            {
                "x": 14.328358208955251,
                "y": 423.7100213219617
            }
        ]
    ],
    [
        [
            {
                "x": 246,
                "y": -401
            },
            {
                "x": 77,
                "y": -283
            },
            {
                "x": -28,
                "y": -201
            },
            {
                "x": 8,
                "y": -65
            },
            {
                "x": 103.36886993603412,
                "y": -220.04264392324092
            },
            {
                "x": 331.5991471215352,
                "y": -168.86993603411514
            },
            {
                "x": 362.30277185501063,
                "y": -99.27505330490402
            },
            {
                "x": 404.26439232409393,
                "y": 164.77611940298505
            },
            {
                "x": 212,
                "y": 121
            },
            {
                "x": -179.1044776119403,
                "y": 4.093816631130096
            },
            {
                "x": -171.94029850746267,
                "y": 173.9872068230277
            },
            {
                "x": 233,
                "y": 301
            },
            {
                "x": 308,
                "y": 296
            },
            {
                "x": 312,
                "y": 428
            }
        ]
    ],
    [
        [
            {
                "x": 47,
                "y": -338
            },
            {
                "x": 324,
                "y": -275
            },
            {
                "x": 38.891257995735714,
                "y": -148.40085287846483
            },
            {
                "x": -133.04904051172707,
                "y": -162.72921108742003
            },
            {
                "x": 481.0234541577827,
                "y": -243.5820895522388
            },
            {
                "x": 438.0383795309169,
                "y": 56.28997867803844
            },
            {
                "x": -27.633262260127935,
                "y": 74.71215351812373
            },
            {
                "x": -89.04051172707887,
                "y": 143.28358208955228
            },
            {
                "x": 254,
                "y": 278
            },
            {
                "x": 355.138592750533,
                "y": 114.62686567164178
            },
            {
                "x": 365.3731343283582,
                "y": 473.85927505330494
            }
        ]
    ],
    [
        [
            {
                "x": 252,
                "y": -452
            },
            {
                "x": 172.96375266524524,
                "y": -315.22388059701495
            },
            {
                "x": -114.62686567164178,
                "y": -260.98081023454154
            },
            {
                "x": 228.2302771855011,
                "y": -231.3006396588486
            },
            {
                "x": 253.81663113006402,
                "y": -52.196162046908285
            },
            {
                "x": 351.04477611940297,
                "y": -74.71215351812367
            },
            {
                "x": -73.68869936034105,
                "y": -188.31556503198294
            },
            {
                "x": -110.53304904051163,
                "y": 76.75906183368875
            },
            {
                "x": -103.36886993603412,
                "y": 249.72281449893387
            },
            {
                "x": 573.1343283582091,
                "y": -170.91684434968016
            },
            {
                "x": -94.15778251599147,
                "y": 335.69296375266526
            },
            {
                "x": 237,
                "y": 394
            }
        ]
    ]
]
cc.Class({
    extends: cc.Component,

    properties: {},

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
       this.init();
       this.node.scale=Math.floor(Math.random()*10+1)/10
    },

    start() {

    },
    init(){
        this.node.movidx = 0;
        var num=Math.floor(Math.random()*movedata.length);
        this.getMovePath(this.node, num)
    },
    getMovePath(sp, moveid) {
        sp.time = Math.floor(Math.random()*2+3);
        sp.moveArr = [];
        sp.moveid = moveid;
        sp.moveMaxidx = movedata[moveid].length;
        sp.runTime = 0;
        let bezier1 = movedata[moveid][sp.movidx];
        sp.moveArr = Bezier(bezier1, 100, sp.time);
        sp.offset = cc.v2(movedata[moveid][sp.movidx][0].x - this.node.x, movedata[moveid][sp.movidx][0].y - this.node.y)
        if (sp.movidx == 0) {
            sp.lastPoint = cc.v2(this.node.x, this.node.y);
        }
    },
    update(dt) {
        if(!GameData.isPause){
            var sp = this.node;
            sp.runTime += dt;
            if (sp != 12 && sp != 13 && sp != 14) {
                if (sp.runTime <= sp.time) {
                    let position1 = sp.moveArr.getPoint(sp.runTime);
                    var curPoint = cc.v2(sp.x, sp.y);
                    sp.lastPoint = curPoint;
                    sp.position = cc.v2(position1.x - sp.offset.x, position1.y - sp.offset.y);
                } else {
                    sp.movidx++;
                    if (sp.movidx >= sp.moveMaxidx) {
                        this.node.y = Math.floor(Math.random() * 100 - 400);
                        this.node.x = Math.floor(Math.random() * 1136 - 568);
                        this.init();
                    }
                }
            }
        }

    },

});
